test_that("fmt_image() works correctly", {

  # Create an input data frame with numbers corresponding to image files
  data_tbl <-
    data.frame(
      a = c(
        "1, 2",
        "3",
        "5, 6,7",
        NA
      ),
      stringsAsFactors = FALSE
    )

  # Get the path to the images
  image_path <- system.file("metro_svg", package = "gt")

  # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset
  tab <- gt(data = data_tbl)

  # Extract vectors from the table object for comparison
  # to the original dataset
  a <- dt_data_get(tab)[["a"]]

  # Expect the extracted values to match those of the original dataset
  expect_equal(data_tbl$a, a)

  # Expect an error when attempting to format a column that does not exist
  expect_error(tab |> fmt_image(columns = b))

  # Format the `a` column to display images
  expect_equal(
    (tab |>
       fmt_image(columns = a, path = image_path, file_pattern = "metro_{x}.svg") |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:2em;vertical-align:middle;\"> <img src=\"\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:2em;vertical-align:middle;\"> <img src=\"\" style=\"height:2em;vertical-align:middle;\"> <img src=\"\" style=\"height:2em;vertical-align:middle;\"></span>",
      NA
    )
  )

  expect_equal(
    (tab |>
       fmt_image(columns = a, path = image_path, file_pattern = "metro_{x}.svg", sep = " / ") |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:2em;vertical-align:middle;\"> / <img src=\"\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:2em;vertical-align:middle;\"> / <img src=\"\" style=\"height:2em;vertical-align:middle;\"> / <img src=\"\" style=\"height:2em;vertical-align:middle;\"></span>",
      NA
    )
  )

  expect_equal(
    (tab |>
       fmt_image(columns = a, path = image_path, file_pattern = "metro_{x}.svg", height = "3em") |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:3em;vertical-align:middle;\"> <img src=\"\" style=\"height:3em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:3em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:3em;vertical-align:middle;\"> <img src=\"\" style=\"height:3em;vertical-align:middle;\"> <img src=\"\" style=\"height:3em;vertical-align:middle;\"></span>",
      NA
    )
  )

  expect_equal(
    (tab |>
       fmt_image(columns = a, path = image_path, file_pattern = "metro_{x}.svg", height = 40) |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:40px;vertical-align:middle;\"> <img src=\"\" style=\"height:40px;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:40px;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"\" style=\"height:40px;vertical-align:middle;\"> <img src=\"\" style=\"height:40px;vertical-align:middle;\"> <img src=\"\" style=\"height:40px;vertical-align:middle;\"></span>",
      NA
    )
  )

  # Create another input data frame with image links
  data_tbl_alt <-
    data.frame(
      a = c(
        "https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Gray.png,https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png",
        "https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-flat.svg"
      ),
      stringsAsFactors = FALSE
    )

  # Create a `gt_tbl` object with `gt()` and the `data_tbl_alt` dataset
  tab_alt <- gt(data = data_tbl_alt)

  # Format the `a` column to display images
  expect_equal(
    (tab_alt |>
       fmt_image(columns = a) |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Gray.png\" style=\"height:2em;vertical-align:middle;\"> <img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-flat.svg\" style=\"height:2em;vertical-align:middle;\"></span>"
    )
  )

  expect_equal(
    (tab_alt |>
       fmt_image(columns = a, sep = " / ") |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Gray.png\" style=\"height:2em;vertical-align:middle;\"> / <img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-flat.svg\" style=\"height:2em;vertical-align:middle;\"></span>"
    )
  )

  expect_equal(
    (tab_alt |>
       fmt_image(columns = a, height = "3em") |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Gray.png\" style=\"height:3em;vertical-align:middle;\"> <img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png\" style=\"height:3em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-flat.svg\" style=\"height:3em;vertical-align:middle;\"></span>"
    )
  )

  # Create another input data frame with image names
  data_tbl_alt_2 <-
    data.frame(
      a = c(
        "RStudio-Logo-Gray.png,RStudio-Logo.png",
        "RStudio-Logo-flat.svg"
      ),
      stringsAsFactors = FALSE
    )

  # Create a `gt_tbl` object with `gt()` and the `data_tbl_alt_2` dataset
  tab_alt_2 <- gt(data = data_tbl_alt_2)

  url_path_1 <- "https://www.rstudio.com/wp-content/uploads/2018/10/"
  url_path_2 <- "https://www.rstudio.com/wp-content/uploads/2018/10"

  # Format the `a` column to display images
  expect_equal(
    (tab_alt_2 |>
       fmt_image(columns = a, path = url_path_1) |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10//RStudio-Logo-Gray.png\" style=\"height:2em;vertical-align:middle;\"> <img src=\"https://www.rstudio.com/wp-content/uploads/2018/10//RStudio-Logo.png\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10//RStudio-Logo-flat.svg\" style=\"height:2em;vertical-align:middle;\"></span>"
    )
  )

  expect_equal(
    (tab_alt_2 |>
       fmt_image(columns = a, path = url_path_2) |>
       render_formats_test(context = "html"))[["a"]],
    c(
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Gray.png\" style=\"height:2em;vertical-align:middle;\"> <img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo.png\" style=\"height:2em;vertical-align:middle;\"></span>",
      "<span style=\"white-space:nowrap;\"><img src=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-flat.svg\" style=\"height:2em;vertical-align:middle;\"></span>"
    )
  )

  # Expect that a column with NAs will work fine with `fmt_image()`,
  # it'll just produce NA values
  na_col_tbl <- dplyr::tibble(a = rep(NA_real_, 10), b = 1:10) |> gt()

  # Expect a returned object of class `gt_tbl` with various
  # uses of `fmt_image()`
  expect_no_error(
    na_col_tbl |> fmt_image(columns = a) |> as_raw_html()
  )

  expect_no_error(
    na_col_tbl |>
      fmt_image(columns = a, rows = 1:5) |> as_raw_html()
  )
})
